package com.weatherquery.servlet;

import com.weatherquery.informbean.WeatherData;
import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Natural-Pride
 * @ProjectName weatherquery
 * @Date 2023/12/23 21:20
 * @Description
 */

@WebServlet("/WeatherSaveServlet")
public class WeatherSaveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String cityId=request.getParameter("cityId");
        String city=request.getParameter("city");
        String nowWeather=request.getParameter("nowWeather");
        String nowTemp= request.getParameter("nowTemp");
        String nowWindClass= request.getParameter("nowWindClass");
        String nowWindDir=request.getParameter("nowWindDir");
        String nowUptime= request.getParameter("nowUptime");
        String forecastDate = request.getParameter("forecastDate");
        String forecastWeek=request.getParameter("forecastWeek");
        String forecastWeather=request.getParameter("forecastWeather");
        String forecastHigh=request.getParameter("forecastHigh");
        String forecastLow=request.getParameter("forecastLow");

        WeatherData weatherData = new WeatherData(cityId, city, nowWeather, nowTemp, nowWindClass, nowWindDir,nowUptime,
                forecastDate, forecastWeek, forecastWeather, forecastHigh, forecastLow);
        WeatherService weatherService = new WeatherService();
        boolean b= weatherService.saveWeather(weatherData);
        String tips = b ? "<label style='color:green'>天气添加成功</label>" : "<label style='color:red'>天气添加失败</";
        //将 tips保存到 request中
        request.setAttribute("tips", tips);
        //转发请求到 prompt.jsp页面
        request.getRequestDispatcher("prompt.jsp").forward(request, response);
    }

}
